package com.starlink.model.dtomapper;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.starlink.model.customdo.Question;
import com.starlink.model.domain.SchoolbagReadTestingDO;
import com.starlink.model.request.SchoolbagReadTestingRequest;
import com.starlink.model.vo.SchoolbagReadTestingVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * @author zhanghong
 */
@Mapper(componentModel = "spring", imports = {JSON.class, Optional.class, Lists.class, Question.class})
public interface SchoolbagReadTestingDtoMapper {

    SchoolbagReadTestingDtoMapper INSTANCE = Mappers.getMapper(SchoolbagReadTestingDtoMapper.class);

    /**
    * DO to VO
    * @param schoolbagReadTestingDO
    * @return
    */
    @Mappings({
            @Mapping(target = "testingRecord", expression = "java(Optional.ofNullable(schoolbagReadTestingDO.getTestingRecord()).map(item -> JSON.parseArray(item, Question.class)).orElse(Lists.newArrayList()))"),
    })
    SchoolbagReadTestingVO do2VO(SchoolbagReadTestingDO schoolbagReadTestingDO);

    /**
     * request to DO
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(target = "testingRecord", expression = "java(Optional.ofNullable(request.getTestingRecord()).map(JSON::toJSONString).orElse(null))"),
    })
    SchoolbagReadTestingDO request2DO(SchoolbagReadTestingRequest request);
}
